﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF_DbCenter.SqlServer.SomeFunctions;
using XLCS.Common;

namespace WPF_DbCenter.SqlServer
{
    /// <summary>
    /// Interaction logic for SqlServerMain.xaml
    /// </summary>
    public partial class SqlServerMain : Page
    {
        public SqlServerMain()
        {
            InitializeComponent();
            //this.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, new KeyGesture(Key.Back))); 
#if DEBUG
            
#else
            button1.Visibility = Visibility.Hidden;
            button2.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
#endif
        }

        /// <summary>
        /// Connect sql server with connection string 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butConnect_Click(object sender, RoutedEventArgs e)
        {
            string connStr = string.Empty;
            connStr = this.textBox1.Text;
            if (!string.IsNullOrEmpty(connStr))
            {
                CoreEA.CoreE c = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer);
                try
                {
                    c.X_Handler.Open(connStr);

                    if (c.X_Handler.IsOpened)
                    {
                        SqlServer.SqlServerIDE ie = new WPF_DbCenter.SqlServer.SqlServerIDE(c.X_Handler);
                        ie.Show();
                    }
                    else
                    {
                        MessageBox.Show(c.X_Handler.LastErrorMsg);
                    }
                }
                catch (Exception ee)
                {
                    XLCS.Common.ProcessException.DisplayErrors(ee);
                }
            }
        }
        /// <summary>
        /// only Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.textBox1.Text = @"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|data.mdf;User Instance=true";
        }

        /// <summary>
        /// only Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.textBox1.Text = "Data Source={0};Initial Catalog={1};User Id={2};Password={3};Trusted_Connection={4}";
        }

        /// <summary>
        /// Connect to Sql server with para defined in CoreEA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butOpenSqlServerWithPara_Click(object sender, RoutedEventArgs e)
        {
            WPFCommonControl.SqlServerLoginControl.X_CollectionData controlResult =
                this.sqlServerLoginControl1.X_Result;

            if (string.IsNullOrEmpty(controlResult.Server) || string.IsNullOrEmpty(controlResult.DbName)
                || string.IsNullOrEmpty(controlResult.UID))
            {
                MessageBox.Show("Please input each element");
                return;
            }


            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
            CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(controlResult.Server, controlResult.DbName, "", controlResult.UID, controlResult.PWD);
            info.CurSqlServerVersion = controlResult.CurType.CurSqlType;
            info.X_Port = controlResult.Port.ToString();
            info.IsTrustedConn = controlResult.IsTrust;

            try
            {
                core.Open(info);
                if (core.IsOpened)
                {
                    SqlServer.SqlServerIDE ide = new SqlServerIDE(core);
                    ide.Show();
                }
            }
            catch (Exception ee)
            {
                XLCS.Common.ProcessException.DisplayErrors(ee);
            }

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            ConnectionString_Help ch = new ConnectionString_Help();
            ch.Show();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            WPFCommonControl.SqlServerLoginControl.X_CollectionData controlResult =
    this.sqlServerLoginControl1.X_Result;

            if (string.IsNullOrEmpty(controlResult.Server) || string.IsNullOrEmpty(controlResult.DbName)
                || string.IsNullOrEmpty(controlResult.UID))
            {
                MessageBox.Show("Please input each element");
                return;
            }

            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
            CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(controlResult.Server, controlResult.DbName, "", controlResult.UID, controlResult.PWD);
            info.CurSqlServerVersion = controlResult.CurType.CurSqlType;
            info.X_Port = controlResult.Port.ToString();
            info.IsTrustedConn = controlResult.IsTrust;

            try
            {
                core.Open(info);
                if (core.IsOpened)
                {
                 //   core.DeleteDuplicateData("TestTable", "ItemID", "Name");
                    core.SelectDataFromNtoNByPrimiaryKey("TestTable", "ItemID", 4, 6);
                    MessageBox.Show("Test OK");
                }
            }
            catch (Exception ee)
            {
                XLCS.Common.ProcessException.DisplayErrors(ee);
            }
        }

        private void butEnumSqlserver_Click(object sender, RoutedEventArgs e)
        {
            EnumSqlServer eServer = new EnumSqlServer();
            eServer.SelectedSqlServer += new EventHandler(eServer_SelectedSqlServer);
            eServer.ShowDialog();
        }

        void eServer_SelectedSqlServer(object sender, EventArgs e)
        {
            this.sqlServerLoginControl1.SetServerName(((MyEventArgs)e).MyText);
        }
    }
}
